//
//  ShopDetailViewController.m
//  GoShops
//
//  Created by zcj on 15-11-30.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "ShopDetailViewController.h"
#import "Grouble.h"
#import "GetUrlString.h"
#import "UITableView+JFTableView.h"
#import "SelectFindHttp.h"
#import "ShopModelDecal.h"
#import "ShopDetailTopImgTableViewCell.h"
#import "ArrayGroupModel.h"
#import "FiveTableViewCell.h"
#import "OtherShopDetailTableViewCell.h"


@interface ShopDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSMutableArray *shopDetailData;

@property (nonatomic,strong)NSMutableArray *shopDetailOtherData;

@property(nonatomic, weak)UITableView *firstTableView;

@end

@implementation ShopDetailViewController
- (NSMutableArray *)shopDetailData
{
    if (_shopDetailData == nil) {
        _shopDetailData = [NSMutableArray array];
    }
    return _shopDetailData;
}
- (NSMutableArray *)shopDetailOtherData
{
    if (_shopDetailOtherData == nil) {
        _shopDetailOtherData = [NSMutableArray array];
    }
    return _shopDetailOtherData;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initNav];
    [self initData];

    // Do any additional setup after loading the view.
}
- (void)initData
{
    self.firstTableView = [UITableView initWithTableView:CGRectMake(0, 0, ScreenWidth, ScreenHeight) withDelegate:self];
    [self.view addSubview:self.firstTableView];
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //[self loadTopData];
        // 附近团购
        [self loadOtherData];
    });
}
- (void)loadOtherData
{
    NSString *urlStr =  [[GetUrlString sharedManager]urlWithMerchantAroundGroupPurchaseData:self.poiid];
    ZCJLog(@"%@",urlStr);
    [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
        ZCJLog(@"%@",json);
        NSDictionary *dataDic = [json objectForKey:@"data"];
        NSMutableArray *dealsArray = [dataDic objectForKey:@"deals"];
        if (dealsArray.count != 0) {
            [self.shopDetailOtherData removeAllObjects];
            for (NSDictionary * dict  in dealsArray) {
                ArrayGroupModel * arrayModel = [ArrayGroupModel objectWithKeyValues:dict];
                [self.shopDetailOtherData addObject:arrayModel];
            }
        }
        [self.firstTableView reloadData];
    } failture:^(id error) {
        
    }];
}
// 获取顶部的数据
- (void)loadTopData
{
     NSString *urlStr =  [[GetUrlString sharedManager]urlWithMerchantDetail:self.poiid];
    [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
        ZCJLog(@"%@",json);
         NSMutableArray *dataArray = [json objectForKey:@"data"];
        if(dataArray.count != 0){
            [self.shopDetailData removeAllObjects];
            for (NSDictionary * dict in dataArray) {
                ShopModelDecal * shopDecal = [ShopModelDecal objectWithKeyValues:dict];
                [self.shopDetailData addObject:shopDecal];
            }
        }
    } failture:^(id error) {
        ZCJLog(@"%@",error);
    }];
}
- (void)initNav
{
    self.title = @"商家详情";
    //返回
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 23, 23);
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(OnBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];

}
- (void)OnBackBtn:(UIButton *)btn
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    //[self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ZCJLog(@"%d",self.shopDetailOtherData.count);
    return 2 + self.shopDetailOtherData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ShopDetailTopImgTableViewCell * cell = [ShopDetailTopImgTableViewCell ShopDetailTopImgTableViewCell:tableView];
        
        return cell;
    }else if (indexPath.row == 1){
        FiveTableViewCell * cell = [FiveTableViewCell FiveTableViewCell:tableView];
        cell.topName = @"附近";
        return cell;
    }else{
        
        OtherShopDetailTableViewCell * cell = [OtherShopDetailTableViewCell OtherShopDetailTableViewCell:tableView];
        ZCJLog(@"%d",indexPath.row);
        if (self.shopDetailOtherData.count != 0) {
            cell.arrayGroup = self.shopDetailOtherData[indexPath.row -2];
        }
        return cell;
    
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 165;
    }else if(indexPath.row == 1){
        return 60;
    }
    return 120;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
